package model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author Manseer Marcel  
 * @author Plavsic Maja
 * @author Pretsch Paul
 */
public class MemoryDataBuffer extends BaseDataBuffer{
	
	private List<TagData> data;
	
	public MemoryDataBuffer()
	{
		super();
		this.data = new ArrayList<TagData>(); 
	}
	
	/* (non-Javadoc)
	 * @see model.BaseDataBuffer#getTagData(java.util.Date, java.util.Date, java.lang.String[])
	 */
	@Override
	protected List<TagData> getTagData(Date from, Date to, String... tags) {
		from = Tools.TruncateTime(from);
		to = Tools.TruncateTime(to);
		
		List<TagData> tmp = new ArrayList<TagData>();
		
		for (Iterator<TagData> i = this.data.iterator(); i.hasNext();) {
			TagData tagData = (TagData) i.next();
			
			if ((tagData.getDate().compareTo(from) == 0) || 
				(tagData.getDate().compareTo(to) == 0) ||
				(tagData.getDate().compareTo(from) * tagData.getDate().compareTo(to) < 0)) {
			
				for (String tag : tags) {
					if (tagData.getTag().compareToIgnoreCase(tag) == 0) {
						tmp.add(tagData);
						break;
					}
				}
			}	
		}
		
		return Collections.unmodifiableList(tmp);
	}

	/* (non-Javadoc)
	 * @see model.BaseDataBuffer#addData(model.TagData)
	 */
	@Override
	protected void addData(TagData tagData) {
		data.add(tagData);		
	}

	/* (non-Javadoc)
	 * @see model.BaseDataBuffer#removeData(model.TagData)
	 */
	@Override
	protected void removeData(TagData tagData) {
		data.remove(tagData);
	}
	
}
